<!--智慧消防V1.0-edit页面-->
<template>
    <div class="app-container">
        <div class="body-title">设备类型{{createStatus?'新增':'编辑'}}</div>
        <el-row :gutter="10">
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                <el-form :model="equipmentTypeForm" ref="equipmentTypeForm" :rules="equipmentTypeFormRules" label-width="100px">
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                        <el-form-item label="设备类型名称" prop="name">
                            <el-input type="text" v-model="equipmentTypeForm.name" placeholder="请输入设备类型名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                      <el-form-item label="设备类型编码" prop="code">
                        <el-input type="text" v-model="equipmentTypeForm.code" placeholder="请输入设备类型编码" clearable></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">

                      <el-form-item label="维保方式" prop="isVideo">
                        <el-select style="width: 100%" v-model="equipmentTypeForm.isVideo" ref="selectUpResId" placeholder="请选择">
                          <el-option
                              v-for="dict in maintenanceOptions"
                              :key="dict.dictValue"
                              :label="dict.dictLabel"
                              :value="dict.dictValue"
                          />
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                      <el-form-item label="是否联动设备" prop="isLinked">
                        <el-select style="width: 100%" v-model="equipmentTypeForm.isLinked" ref="selectUpResId" placeholder="请选择">
                          <el-option
                              v-for="dict in yesOrNoOptions"
                              :key="dict.dictValue"
                              :label="dict.dictLabel"
                              :value="dict.dictValue"
                          />
                        </el-select>
                      </el-form-item>
                    </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                    <el-form-item label="维保周期" prop="cycle">
                      <el-select style="width: 100%" v-model="equipmentTypeForm.cycle" ref="selectUpResId" placeholder="请选择维保周期">
                        <el-option
                            v-for="dict in scycleOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                    <el-form-item label="检查设备百分比" prop="inspectPercent">
                      <el-input style="width: 95%" type="text" v-model="equipmentTypeForm.inspectPercent" placeholder="请输入检查百分比" clearable></el-input>%
                    </el-form-item>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                    <el-form-item label="检测设备百分比" prop="jcPercent">
                      <el-input style="width: 95%" type="text" v-model="equipmentTypeForm.jcPercent" placeholder="请输入检查百分比" clearable></el-input>%
                    </el-form-item>
                  </el-col>
                </el-form>
            </el-col>
            <div style="text-align: center" class="dialog-footer">
                <el-button @click="cancelCycle">取 消</el-button>
                <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createCycle">创 建</el-button>
                <el-button :loading="addLoading" v-else type="primary" @click="updateCycle">修 改</el-button>
            </div>
        </el-row>
    </div>
</template>

<script>
import {addData, getData, updateData} from "@/api/equipmentType";
import {isExit} from "@/api/validator";

export default {
    name: "CycleEdit",
    data(){
      let zzNum = (rule, value, callback) => {
        const reg = /^(?!00)(?:[0-9]{1,2}|100)$/
        if (reg.test(value)) {
          return callback();
        }
        callback('请输入0-100的整数');
      }
        return{
            equipmentTypeForm:{
              id: "",
              ucId: "",
              name: "",
              code: "",
              isVideo: "",
              isLinked: "",
              inspectPercent: 0,
              jcPercent: 0,

            },
            equipmentTypeFormRules:{
              inspectPercent: [
                {validator: zzNum, required: true, trigger: 'blur'}
              ],
              jcPercent: [
                {validator: zzNum, required: true, trigger: 'blur'}
              ]
            },
            //父级资源数据
            yesOrNoOptions:[],
          scycleOptions:[],
          maintenanceOptions:[],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            addLoading:false
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId =='0'
        }
    },
    created() {
      this.getDicts("cycle_type").then(response => {
        this.scycleOptions = response.data.data;
      });
      this.getDicts("yes_or_no").then(response => {
        this.yesOrNoOptions = response.data.data;
      });
      this.getDicts("maintenance_type").then(response => {
        this.maintenanceOptions = response.data.data;
      });

      if (!this.createStatus) {
        let ucId = this.$route.params.ucId
        this.getCycle(ucId)
      }
    },
    methods:{
        cancelCycle(){
          window.history.back()
        },
        createCycle(){
            this.addLoading = true
            this.$refs['equipmentTypeForm'].validate(valid => {
                if (valid){
                    addData(this.equipmentTypeForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加设备维保周期成功"
                        })
                        this.cancelCycle()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateCycle(){
          this.addLoading = true
          this.$refs['equipmentTypeForm'].validate(valid => {
            if (valid){

              updateData(this.equipmentTypeForm).then(res=>{
                this.addLoading = false
                this.$notify.success({
                  title: '成功',
                  message: "修改设备类型成功"
                })
                this.cancelCycle()
              }).catch(res=>{
                this.addLoading = false
                this.$notify.error({
                  title: '失败',
                  message: res.msg
                })
              })
            }else {
              this.addLoading = false
              console.log("表单验证不通过")
            }
          })
        },
        getCycle(value){
          getData(value).then(res => {
            this.equipmentTypeForm = res.data.data
          }).catch(res => {

          })
        }
    }
}
</script>

<style scoped>

</style>
